<style type="text/css" title="currentStyle">
	@import "../media/css/demo_table.css";
	@import "media/css/TableTools.css";
</style>

<script type="text/javascript" charset="utf-8" src="../media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="media/ZeroClipboard/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/TableTools.js"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready( function () {
		/* You might need to set the sSwfPath! Something like:
		 *   TableToolsInit.sSwfPath = "/media/swf/ZeroClipboard.swf";
		 */
		$('#example').dataTable( {
			"sDom": 'T<"clear">lfrtip'
		} );
	} );


	function showFormDiv()
{
	if(document.getElementById('formDiv').style.display=='none')
	{
		document.getElementById('formDiv').style.display = 'block';
		document.getElementById('shLink').innerHTML = "Hide Form";
	}
	else
	{
		document.getElementById('formDiv').style.display = 'none';
		if(document.getElementById('shLink'))
		document.getElementById('shLink').innerHTML = "Show Form";
	}
}
function showPreloader()
{
	var returnVal = validateMapReport()
	if(returnVal == 1)
	{
		document.getElementById('popup_div').innerHTML = '<div id="loading_txt" >Loading...</div>';
		document.frm_map_filter.submit();
	}
}

function hidePreLoader()
{
	document.getElementById('popup_div').innerHTML = '&nbsp;';
}
</script>
<?php
@set_time_limit(0);

function chk_folder($filename)
{
	$fp_load = @fopen("$filename", "rb");
	if ( $fp_load )
	{
		return true;
	}
	else
	{
		return false;
	}
}

if($recordUserInfo[ci_clientType] == "Client" && $recordUserInfo[ui_isAdmin] == "1")
{
	$devices_query =  "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_clientId =".$_SESSION[clientID]." AND di_status = 1 ORDER BY di_deviceName,di_deviceId ASC";
}
else if($recordUserInfo[ci_clientType] == "Client" && $recordUserInfo[ui_isAdmin] == "0" && $recordUserInfo[ui_roleId] == "1")
{
	$devices_query = "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_status = 1 AND di_clientId=".$_SESSION[clientID]." AND di_assignedUserId = ".$_SESSION[userID]." ORDER BY di_deviceName,di_deviceId ASC";
} else if($recordUserInfo[ci_clientType] == "Reseller")
	{ //echo '<pre>'; print_r($_SESSION);echo '</pre>';
	$devices_query = "SELECT * FROM tb_deviceinfo, tb_clientinfo WHERE di_status =1 AND ci_clientId =".$_SESSION[userID]." ORDER BY ci_clientName, di_deviceName, di_deviceId ASC";
	}

$devices_resp = mysql_query($devices_query);

?>

<script type="text/javascript" language="javascript">


function validateMapReport()
{
	/*if(document.getElementById('map_device_id').value== 0 )
	{
	alert("Select Device"); 
	document.getElementById('map_device_id').focus();
	return false;
	} 

	return true;*/
}

var request = false;
if (!request && typeof XMLHttpRequest != 'undefined') {
request = new XMLHttpRequest();
}
function fillSelect(client){
    var url = "device_names.php?client=" + escape(client);
    request.open("GET",url,true);
    request.onreadystatechange = go;
    request.send(null);
}
function go(){
    if(request.readyState == 4){
        if(request.status == 200){
            var response = request.responseText;
            var list = document.getElementById('map_device_id');
            var devices = response.split('|');
            var i;
            for(i=0;i<devices.length;i++){
				 var nm = devices[i].split('$');
                var x = document.createElement('option');
				x.value = nm[0];
                var y = document.createTextNode(nm[1]);
                x.appendChild(y);
                list.appendChild(x);
            }
        }
    }
}
function initCs(){
    var client = document.getElementById('client_name');
    
    client.onchange = function(){
        if(this.value!=""){
            var list = document.getElementById('map_device_id');
             while(list.childNodes[0]){
                 list.removeChild(list.childNodes[0]);
               fillSelect(this.value);
             }
        }
        fillSelect(client.value);
    }
}
window.onload = initCs;

$(function() {
	$( "#from_date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		maxDate: 0,
		showOn: "button",
		buttonImage: "images/calendar.gif",
		buttonImageOnly: true,
		dateFormat : "yy-mm-dd"
	});
});
$(function() {
	$( "#to_date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		maxDate: 0,
		showOn: "button",
		buttonImage: "images/calendar.gif",
		buttonImageOnly: true,
		dateFormat : "yy-mm-dd"
	});
});

</script>
<div id="formDiv">
<form id="frm_map_filter" name="frm_map_filter" method="post" action="" onSubmit="return validateMapReport();">      	 
<table class="gridform">
<tr><th colspan="4">Subscription Report</th></tr>

  <tr>
      <?php
      $query3 = mysql_query("SELECT * FROM tb_clientinfo WHERE ci_clientType='Client' AND ci_clientId=".$_SESSION[clientID]." ORDER BY ci_clientName ASC");
      ?>
    <td><span class="form_text">Select Client</span></td>
    <td>
        <select name="client_name" id="client_name">
            <option value="">Select Client</option>
         <?php while($noticia2 = @mysql_fetch_assoc($query3)) {?>
            <option <?php if($noticia2[ci_id]==$_POST[client_name]){echo 'selected';}?> value="<?php echo $noticia2[ci_id];?>"><?php echo $noticia2[ci_clientName];?></option>
         <?php }?>
        </select>
    </td>


    <td><span class="form_text">Select Device</span></td>
   <td> <select name="map_device_id" id="map_device_id" tabindex="1" style="width:100%" >
        
        </select>
    </td>
  
  </tr>              
 
<tr>
    <td width="15%" align="right"><span class="form_text">Renewal From Date</span></td>
    <td width="35%" align="left">
    <input type="text" name="from_date" id="from_date" readonly="true" size="12" tabindex="2" value="<?php echo $_POST[from_date]; ?>" style="width:140px;"/>&nbsp;
    </td>
    <td width="15%" align="right"><span class="form_text">To Date</span></td>
    <td width="35%">
    <input type="text" name="to_date" id="to_date" size="12" style="width:140px;" tabindex="4" readonly="true" value="<?php echo $_POST[to_date]; ?>" />&nbsp;
    </td>
  </tr>
  <tr>
    <td height="33" colspan="4" align="center">
    <input type="submit" name="map_filter_btn"   value="Filter" class="click_btn" tabindex="5" />
    <input type="button" name="map_cancel_btn" id="map_cancel_btn" value="Reset" class="click_btn" onClick="location.href='index.php?ch=subscription_report';" tabindex="6" /> 
   
    </td>
  </tr>

 
</table>
</form>
</div>
<div id="popup_div" style=" display:block; border:0px;" >

</div>

<?php
if(isset($_POST[map_filter_btn]) && $_POST[map_filter_btn]!=""){?>

<script type="text/javascript">
showFormDiv();
hidePreLoader();
</script>
<div style="overflow:scroll; overflow-X:hidden;  border:1px solid #dfe9ed; border-top:0px solid #FFF;">
<div class="listofusers" align="right" style="padding-right:10%"><a href="#" class="error_strings" id="shLink" onClick="showFormDiv();">Show Form</a></div>

<?php

/*for($i=0;$i<=count($_POST[map_device_id]);$i++){
	if($_POST[map_device_id][$i]!=""){
    $dev_id = explode('#',$_POST[map_device_id][$i]);
    $device_id[] = $dev_id[1];
	}

}
print_r($device_id); echo '<br>';
$all_dev = implode("','",$device_id);*/
$where = "";
if($_POST['client_name']!=""){
$where .= ("tcs_clientId ='".$_POST['client_name']."'");
}

if($_POST['map_device_id']!=""){
 $where .= (" and tcs_deviceId ='".$_POST['map_device_id']."'");
}

if($_POST[from_date] !="" && $_POST[to_date]!=""){
	if($where!=""){$d = 'and';}
  $where .= " ".$d." tcs_renewalDateFrom between '".$_POST[from_date]."' and '".$_POST[to_date]."' ";
}

if($where!=""){

     $devSubInfo = "select tcs_deviceId,tcs_clientId,tcs_renewalDateFrom,tcs_reminderDays,tcs_amount,tcs_payType,tcs_noOfMonths from tb_client_subscription,tb_deviceinfo where ".$where." and tcs_deviceId=di_id";
   
  $devSubresp = mysql_query($devSubInfo);
  ?>
<table cellspacing="0" cellpadding="0" border="0" id="example" class="gridform_final">
<thead>
<tr>
     <th>Client Name</th>
        <th>Device Id   </th>
        <th>Renewal Date</th> 
        <th>Expiry Date</th>
        <th>Reminder Days</th>
        <th>Amount <br /> Type</th>
  </tr>
  </thead>
  <?php
	  $i=1;
  while($devsubResult = mysql_fetch_array($devSubresp)){
 /*echo "select ui_username,ui_mobile,ci_clientName from tb_userinfo,tb_clientinfo where ui_id='".$devsubResult[tcs_clientId]."' and ui_id=ci_id";*/
	 
 $query = mysql_query("select ui_username,ui_mobile from tb_userinfo where ui_id='".$devsubResult[tcs_clientId]."'");
   $result = mysql_fetch_row($query);
    // echo '<pre>'; print_r($dev_id);echo '</pre>';

 $query1 = mysql_query("select di_deviceId,di_deviceName from tb_deviceinfo where di_id='".$devsubResult[tcs_deviceId]."'");
   $result1 = mysql_fetch_row($query1);
	if($result1[0]!=""){
		$dev_nm = $result1[0];
	} else {
        $dev_nm = $result1[1];
	}

	  echo '<tr>';
  	  echo '<td>'.$result[0].'</td>';
  	  echo '<td>'.$dev_nm.'</td>';
 	  echo '<td>'.date("d-m-Y",strtotime($devsubResult[tcs_renewalDateFrom])).'</td>';
	  echo '<td>'.date("d-m-Y",strtotime("-1 days ".($devsubResult[tcs_noOfMonths]) ."months ".$devsubResult[tcs_renewalDateFrom]))."(".$devsubResult[tcs_noOfMonths]." Months)".'</td>';
	  echo '<td>'.$devsubResult[tcs_reminderDays].'</td>';
	  echo '<td>'.$devsubResult[tcs_amount].'<br />('.$devsubResult[tcs_payType].')</td>';
	  echo '</tr>';
	  $i++;

  }?>

<?php }else {
	
 echo '<tr><td>Please select any criteria.</td></tr>';	
}
echo '</table>';
} ?>

